/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package elaborate.tag_analysis.client.utils.resource_logs;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author lendle
 */
public class ResourceLogs {

    protected List<Category> categories = new ArrayList<Category>();
    private Map<String, Category> categoryCache=new HashMap<String, Category>();
    private Map<String, ResourceLog> resourceCache=new HashMap<String, ResourceLog>();
    
    public void clear(){
        this.categories.clear();
        this.categoryCache.clear();
        this.resourceCache.clear();
    }
    /**
     * Get the value of categories
     *
     * @return the value of categories
     */
    public List<Category> getCategories() {
        return categories;
    }

    /**
     * Set the value of categories
     *
     * @param categories new value of categories
     */
    public void setCategories(List<Category> categories) {
        this.categories = categories;
    }

    public Category getCategory(String id){
        if(categoryCache.containsKey(id)){
            return categoryCache.get(id);
        }
        for(Category category : this.categories){
            categoryCache.put(category.getId(), category);
            if(id.equals(category.getId())){
                return category;
            }
        }
        return null;
    }
    
    public ResourceLog getResource(String categoryId, String id){
        String key=categoryId+"::"+id;
        if(resourceCache.containsKey(key)){
            return resourceCache.get(key);
        }
        for(Category category : this.categories){
            for(ResourceLog resource : category.getResources()){
                resourceCache.put(categoryId+"::"+resource.getId(), resource);
                if(id.equals(resource.getId())){
                    return resource;
                }
            }
        }
        return null;
    }
}
